package se.lth.student.axisandroidcam.client.cameraclients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;

import se.lth.student.axisandroidcam.client.AxisAndroidCamera;
import se.lth.student.axisandroidcam.client.Camera;
import se.lth.student.axisandroidcam.client.Toolbox;

public class NetworkClientOut extends ClientOut {
	private Socket clientSocket;
	private String serverAddress;
	private OutputStream output;
	private byte[] command;
	private BufferedReader in;
	private PrintWriter out;
	private int mode;

	public NetworkClientOut(int cam, Activity activity, String serverAddress) {
		super(cam, activity );
		this.serverAddress = serverAddress;
		command = new byte[1];
		mode = 0;
	}

	@Override
	public void run() {
		Toolbox.printDebug("Starting Client out");

		try {
			clientSocket = new Socket(serverAddress, AxisAndroidCamera.PORT
					+ (cam * 10) + 1);
			Toolbox.printDebug("CameraID-" + cam);
			output = clientSocket.getOutputStream();
			Toolbox.printDebug("NetworkOut: Connected to: " + serverAddress);
			out = new PrintWriter(clientSocket.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			while (in.readLine() != null) {

			}
		} catch (UnknownHostException e) {
			Toolbox.printDebug("NetworkOut: Unknown host: " + serverAddress);
			e.printStackTrace();

		} catch (IOException e) {
			Toolbox.printDebug("NetworkOut: IOException: " + serverAddress);
			e.printStackTrace();
		}

	}

	public synchronized void setMovieMode(int movie) {
		Toolbox.printDebug("Changing mode");
		mode = movie;
		if (mode == 1) {
			out.println("MOVIE");
		} else {
			out.println("IDLE");
		}
	}
}
